﻿Imports System.Data.OleDb

Public Class AddClub
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnAddClub_Click(sender As Object, e As EventArgs) Handles btnAddClub.Click
        If Page.IsValid Then

            Dim newFileName As String

            If fuEmblem.HasFile Then
                Dim myGUID As New System.Guid
                myGUID = Guid.NewGuid
                newFileName = myGUID.ToString + ".jpg"
                Dim picLocationOnServerHardDisk = Request.MapPath("Emblems") & "/" & newFileName
                fuEmblem.SaveAs(picLocationOnServerHardDisk)
            Else
                newFileName = "noemblem.jpg"
            End If

            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
            Dim createClubSQL As String = "INSERT INTO Clubs(ClubName,FoundationDate,Address,CountryID_FK,Emblem,Username,CreatedAt,UpdatedAt) VALUES (@f1,@f2,@f3,@f4,@f5,@f6,@f7,@f8)"
            Dim createClubCmd As OleDbCommand = New OleDbCommand(createClubSQL, oleDbConn)
            createClubCmd.CommandType = CommandType.Text
            createClubCmd.Parameters.AddWithValue("@f1", txtClubName.Text)
            createClubCmd.Parameters.AddWithValue("@f2", txtFoundationDate.Text)
            createClubCmd.Parameters.AddWithValue("@f3", txtClubAddress.Text)
            createClubCmd.Parameters.AddWithValue("@f4", ddlCountry.SelectedValue)
            createClubCmd.Parameters.AddWithValue("@f5", "Emblems/" & newFileName)
            createClubCmd.Parameters.AddWithValue("@f6", User.Identity.Name)
            createClubCmd.Parameters.AddWithValue("@f7", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"))
            createClubCmd.Parameters.AddWithValue("@f8", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"))

            oleDbConn.Open()
            createClubCmd.ExecuteNonQuery()

            System.Threading.Thread.Sleep(500)

            Session("UserMessage") = "The club was successfully added!"
            Response.Redirect("~/ClubList.aspx")

            createClubCmd.Dispose()
            oleDbConn.Close()
            oleDbConn.Dispose()
        Else
            Session("UserMessage_Error") = "There was a problem with the data you submitted. Please try again."
            Response.Redirect(Request.RawUrl)
        End If
    End Sub

    Protected Sub btnBackClubList_Click(sender As Object, e As EventArgs) Handles btnBackClubList.Click
        Response.Redirect("~/ClubList.aspx")
    End Sub
End Class